/**
 * Copyright (c) 2020 铭软科技(mingsoft.net)
 * 本软件及相关文档文件（以下简称“软件”）的版权归 铭软科技 所有
 * 遵循铭软科技《保密协议》
 */
package net.mingsoft.mwebsite.action;

import cn.hutool.core.lang.Assert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.mingsoft.base.entity.ResultData;
import net.mingsoft.mwebsite.biz.IBackUpBiz;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.net.MalformedURLException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * 备份数据控制层
 * @author 上官俊杰
 * @version
 */
@Api(value = "备份数据控制层")
@Controller
@RequestMapping("/${ms.manager.path}/mwebsite/backup")
public class BackUpAction extends BaseAction {

	/**
	 * 备份数据业务层的注入
	 */
	@Autowired
	private IBackUpBiz backUpBiz;


	/**
	 * 备份管理页面
	 * @param request
	 * @param mode
	 * @param response
	 * @return
	 */
	@ApiOperation(value = "主界面")
	@GetMapping("/index")
	public String index(HttpServletRequest request,@ApiIgnore ModelMap mode,HttpServletResponse response){
		return "/mwebsite/backup/index";
	}

	/**
	 * 备份全站数据
	 */
	@ApiOperation(value = "备份全站数据")
	@GetMapping("/backupAll")
	@ResponseBody
	@RequiresPermissions("mwebsite:backup:backupAll")
	public ResultData backupAll(){
		backUpBiz.backupAllTable();
		return ResultData.build().success();
	}

	/**
	 * 备份站点数据
	 */
	@ApiOperation(value = "备份站点数据")
	@GetMapping("/backupSite")
	@ResponseBody
	@RequiresPermissions("mwebsite:backup:backupSite")
	public ResultData backupSite(Integer appId){
		Assert.notNull(appId,"站点Id为空");
		backUpBiz.backupSiteTable(appId);
		return ResultData.build().success();
	}
	/**
	 * 回滚数据
	 */
	@ApiOperation(value = "回滚数据")
	@PostMapping("/rollBack")
	@ResponseBody
	@RequiresPermissions("mwebsite:backup:rollBack")
	public ResultData rollBack(String file) throws MalformedURLException, SQLException {
		Assert.notNull(file,"文件为空");
		backUpBiz.rollBack(file);
		return ResultData.build().success();
	}
	/**
	 * 获取sql文件集合
	 */
	@ApiOperation(value = "获取sql文件集合")
	@GetMapping("/sqlFileList")
	@ResponseBody
	@RequiresPermissions("mwebsite:backup:sqlFileList")
	public ResultData sqlFileList(){
		List<Map<String, String>> sqlFileList =  backUpBiz.getSqlFileList();
		return ResultData.build().success(sqlFileList);
	}

}
